<?php

require FCPATH . '/application/controllers/backend/backend.php';

class City extends Backend {

	public function __construct() {
		parent::__construct();
		$this->load->model('city_model', 'model');
	}

	public function index() {
		$data = array(
			'l_position' => array(
				array('name' => '城市管理')
			)
		);
		$this->load_js('backend_city');
		$this->load_css('backend_city');
		$this->rend('city', $data);
	}

	public function records() {
		$page = intval($this->input->get('page'));
		$page_size = intval($this->input->get('pageSize'));
		$page = $page ? $page : 1;
		$page_size = $page_size ? $page_size : 10;
		$offset = $page_size * ($page - 1);

		$conditions = array();
		$id = intval($this->input->get('id'));
		if (!empty($id)) {
			$conditions[] = array('where', 'id', $id);
		}

		$type = intval($this->input->get('type'));
		if (!empty($type)) {
			$conditions[] = array('where', 'type', $type);
		}

		$position_x = intval($this->input->get('position_x'));
		if (!empty($position_x)) {
			$conditions[] = array('where', 'position_x', $position_x);
		}

		$position_y = intval($this->input->get('position_y'));
		if (!empty($position_y)) {
			$conditions[] = array('where', 'position_y', $position_y);
		}

		$name_zh = trim($this->input->get('name_zh'));
		if (!empty($name_zh)) {
			$conditions[] = array('like', 'name_zh', $name_zh);
		}

		$name_fr = trim($this->input->get('name_fr'));
		if (!empty($name_fr)) {
			$conditions[] = array('like', 'name_fr', $name_fr);
		}

		$name_en = trim($this->input->get('name_en'));
		if (!empty($name_en)) {
			$conditions[] = array('like', 'name_en', $name_en);
		}

		$start = $this->input->get('start');
		if (!empty($start)) {
			$conditions[] = array('where', 'created_at', $start);
		}

		$end = $this->input->get('end');
		if (!empty($end)) {
			$conditions[] = array('where', 'created_at', $end);
		}

		$order_fields = array('id', 'type', 'position_x', 'position_y', 'name_zh', 'name_fr', 'name_en', 'created_at');
		$order_field = intval($this->input->get('order_field'));
		$order_type = intval($this->input->get('order_type'));
		$orders = array();
		if ($order_field >= 0 && $order_field < count($order_fields)) {
			$orders = array(
				array($order_fields[$order_field], $order_type == 1 ? 'asc' : 'desc')
			);
		}
		$this->json_back('XT001', '', array(
			'total' => $this->model->total($conditions),
			'records' => $this->model->records(array(), $conditions, $orders, $page_size, $offset)
		));
	}

	public function insert() {
		$data = array(
			'type' => intval($this->input->post('type')),
			'position_x' => intval($this->input->post('position_x')),
			'position_y' => intval($this->input->post('position_y')),
			'name_zh' => trim($this->input->post('name_zh')),
			'name_fr' => trim($this->input->post('name_fr')),
			'name_en' => trim($this->input->post('name_en')),
		);

		$this->validate($data);

		if (($id = $this->model->insert($data)) > 0) {
			$this->json_back('XT001', '添加成功', array('id' => $id));
		}

		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	public function update() {
		$id = intval($this->input->post('id'));
		$data = array(
			'type' => intval($this->input->post('type')),
			'position_x' => intval($this->input->post('position_x')),
			'position_y' => intval($this->input->post('position_y')),
			'name_zh' => trim($this->input->post('name_zh')),
			'name_fr' => trim($this->input->post('name_fr')),
			'name_en' => trim($this->input->post('name_en')),
		);

		$this->validate($data, $id);

		if ($this->model->update($id, $data)) {
			$this->json_back('XT001', '更新成功', array('id' => $id));
		}

		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	public function delete() {
		$id = intval($this->uri->segment('4'));
		if ($this->model->delete($id)) {
			$this->json_back('XT001', '删除成功。');
		}
		$this->json_back('XT002', '数据库或SQL语句有误。');
	}

	private function validate($data, $id = 0) {
		if (empty($data['name_zh'])) {
			$this->json_back('XT002', '中文名字不能为空。');
		}

		if (empty($data['name_fr'])) {
			$this->json_back('XT002', '法文名字不能为空。');
		}

		if (empty($data['name_en'])) {
			$this->json_back('XT002', '英文名字不能为空。');
		}

		$conditions = array(
			array('where', 'type', $data['type']),
			array('where', 'position_x', $data['position_x']),
			array('where', 'position_y', $data['position_y'])
		);

		if ($id > 0) {
			$conditions[] = array('where', 'id <>', $id);
		}

		if ($this->model->total($conditions) > 0) {
			$this->json_back('XT002', '该城市的位置和已有的某个城市位置完全一样，请更换位置。');
		}
	}

}
